package com.example.student.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.apiservice.client.TeacherClient;
import com.example.common.domain.Result;
import com.example.common.domain.vo.QuestionVo;
import com.example.student.domain.Dto.AnswerSubmitDTO;
import com.example.common.domain.dto.HomeworkDto;
import com.example.common.domain.vo.PageWithAnswer;
import com.example.common.domain.vo.QuestionWithAnswerVO;
import com.example.student.service.HomeworkService;
import com.example.student.service.StudentAnswersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


@Api(value = "学生端接口", description = "学生查看操作")
@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/students")
@RequiredArgsConstructor
public class HomeWorkController {

    @Autowired
    private TeacherClient teacherClient;
    @Autowired
    private StudentAnswersService studentAnswersService;
    @Autowired
    private HomeworkService homeworkService;


    /**
     * .查看题目接口,包含学生答案
     */
    @GetMapping("/getHomework/{homeworkId}")
    public Result<PageWithAnswer<QuestionWithAnswerVO>> getHomework(@RequestBody HomeworkDto h) {
        Result<Page<QuestionVo>> test = teacherClient.getQuestionsByHomeworkId(h.getHomeworkId(), h.getPageNum(), h.getPageSize());
        Page<QuestionVo> questionPage = test.getData();
        // 2. 转换为带答案的VO列表
        PageWithAnswer<QuestionWithAnswerVO> resultPage = new PageWithAnswer<>();
        BeanUtils.copyProperties(questionPage, resultPage);
        return Result.success(studentAnswersService.getHomeworkWithAnswers(resultPage, h.getHomeworkId(), h.getStudentId()));
    }

    /**
     * 批量提交/更新答案
     */
    @PostMapping("/submitAnswer")
    public Result<String> submitAnswer(@RequestBody AnswerSubmitDTO submitDTO) {
        // 1. 校验参数
        if (submitDTO.getStudentId() == null ||
                submitDTO.getHomeworkId() == null ||
                submitDTO.getAnswers() == null) {
            return Result.error("参数不完整");
        }

        // 2. 处理答案提交
        studentAnswersService.batchSaveOrUpdate(
                submitDTO.getStudentId(),
                submitDTO.getHomeworkId(),
                submitDTO.getAnswers()
        );

        return Result.success("答案提交成功");
    }

    @ApiOperation("作业提交")
    @PostMapping("/submitHomework/{homeworkId}/{studentId}")
    public Boolean submitHomework(@PathVariable String homeworkId, @PathVariable String studentId) {
        return homeworkService.submitHomework(homeworkId, studentId);
    }

//    @ApiOperation("作业状态列表")
//    @GetMapping("/getHomeworkStatus/{homeworkId}")
//    public Result<HomeworkSatatus> getHomeworkStatus(@PathVariable String homeworkId) {
//        return teacherClient.getHomeworkStatus(homeworkId);
//    }


}
